﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using Entity;
using BLL;

namespace Web.admin.User
{
    public partial class List :System.Web.UI.Page
    {
        TeacherService teacherService = new TeacherService();
        AcademyService academyService = new AcademyService();
        const int pagesize = 20;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                RP_Bind();
                AcademyList.DataSource = academyService.getAllAcademies();
                AcademyList.DataTextField = "ACA_NAME";
                AcademyList.DataValueField = "ACA_ID";
                AcademyList.DataBind();
                if (Session["LocalselectValue"] != null && Session["LocalselectValue"].ToString() != "")
                    AcademyList.SelectedValue = Session["LocalselectValue"].ToString();
            }
        }

        /// <summary>
        /// reapeater控件绑定
        /// </summary>
        protected void RP_Bind()
        {
            Pager1.PageSize = pagesize;
            int records = 0;
            if (Session["innerConCom"] != null && Session["innerConCom"].ToString() != "")
            {
                RpList.DataSource = teacherService.getPageCompetitions(Session["innerConCom"].ToString(), " TEA_NAME DESC", Pager1.PageSize, Pager1.PageIndex, out records);
            }
            else
                RpList.DataSource = teacherService.getPageCompetitions("", " TEA_NAME DESC", Pager1.PageSize, Pager1.PageIndex, out records);
            RpList.DataBind();
            //初始化分页条,设置样式
            Pager1.SetPage(records);
            Pager1.ShowPageJump = true;
            Pager1.ShowPageLan = 0; //导航条语言:0=默认，1=中文，2=英语
        }
        protected void AcademyList_SelectedIndexChanged(object sender, EventArgs e)
        {
            string selAcamediyId = AcademyList.SelectedValue;
            if (selAcamediyId == "")
            {
                Session["LocalselectValue"] = "";
                Session["innerConCom"] = "";
            }
            else
            {
                Session["LocalselectValue"] = selAcamediyId;
                Session["innerConCom"] = "ACA_ID='" + selAcamediyId + "'";
            }
            RP_Bind();
        }

        /// <summary>
        /// repeater事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Rp_Event(object sender, RepeaterCommandEventArgs e)
        {
            string TeaId = ((HiddenField)e.Item.FindControl("Hf_id")).Value;
            BoTeachers teacherModel = new BoTeachers() { TEA_ID = TeaId };
            switch (e.CommandName)
            { 
                case "del_User":
                    teacherService.DeleteTeacher(teacherModel);
                    break;
            }
            RP_Bind();
        }
        ///// <summary>
        ///// 对RpList的数据绑定
        ///// </summary>
        //protected void RpList_Bind()
        //{
        //    if (!string.IsNullOrEmpty(DbCommon.Utils.GetCookie("pageSize")))
        //    {
        //        PageSize = int.Parse(DbCommon.Utils.GetCookie("pageSize"));
        //    }
        //    else
        //    {
        //        PageSize = 8;
        //        DbCommon.Utils.writeToCookie("pageSize", PageSize.ToString());
        //    }
        //    TotalCount = dbHelper.GetCount("UserInformation");
        //    PageCount = (TotalCount + PageSize - 1) / PageSize;
        //    if (!string.IsNullOrEmpty(Request.QueryString["start"]))
        //        startIndex = int.Parse(Request.QueryString["start"]);
        //    else
        //        startIndex = 1;
        //    endIndex = (startIndex + PageSize-1) >= TotalCount ? TotalCount : (startIndex + PageSize-1);
        //    if (Session["UserClass"] != null && Session["UserClass"].ToString() != "") {
        //        RpList.DataSource = dbHelper.GetPageTable("UserInformation", startIndex, endIndex, "UfLoginTime desc", "UfClassId=" + Session["UserClass"]);
        //    }
        //    else
        //        RpList.DataSource = dbHelper.GetPageTable("UserInformation", startIndex, endIndex, "UfLoginTime desc", "");
        //    RpList.DataBind();
        //    tb_size.Text = (PageSize).ToString();
        //    lb_pageNum.InnerHtml = editHtmlStr();
        //}
        ///// <summary>
        ///// 编写html语句
        ///// </summary>
        ///// <returns></returns>
        //protected string editHtmlStr()
        //{
        //    StringBuilder stb = new StringBuilder();
        //    if (startIndex == 1)
        //    {
        //        stb.Append("<a class=\"a_btn_CCl a_btn_NCl\" href=\"#\">上一页</a>");
        //    }
        //    else
        //    {
        //        stb.Append("<a class=\"a_btn_CCl\" href=\"List.aspx?start=" + (startIndex - PageSize) + "\">上一页</a>");
        //    }
        //    if (PageCount <= 10)
        //    {
        //        for (int j = 1; j <= PageCount; j++)
        //        {
        //            if ((startIndex+PageSize -1)/ PageSize == j)
        //            {
        //                if (j == 1)
        //                {
        //                    stb.Append("<a class=\"a_num_CCl a_num_NCl\" href=\"List.aspx?start=1\">" + j + "</a>");
        //                }
        //                else
        //                    stb.Append("<a class=\"a_num_CCl a_num_NCl\" href=\"List.aspx?start=" + ((j - 1) * PageSize + 1) + "\">" + j + "</a>");
        //            }
        //            else
        //            {
        //                if (j == 1)
        //                {
        //                    stb.Append("<a class=\"a_num_CCl\" href=\"List.aspx?start=1\">" + j + "</a>");
        //                }
        //                else
        //                    stb.Append("<a class=\"a_num_CCl\" href=\"List.aspx?start=" + ((j - 1) * PageSize + 1) + "\">" + j + "</a>");
        //            }
        //        }
        //    }
        //    else
        //    {
        //        for (int j = 1; j <= 10; j++)
        //        {
        //            if (startIndex / PageSize + 1 == j)
        //            {
        //                if (j == 1)
        //                {
        //                    stb.Append("<a class=\"a_num_CCl a_num_NCl\" href=\"List.aspx?start=1\">" + j + "</a>");
        //                }
        //                else
        //                    stb.Append("<a class=\"a_num_CCl a_num_NCl\" href=\"List.aspx?start=" + ((j - 1) * PageSize + 1) + "\">" + j + "</a>");
        //            }
        //            else
        //            {
        //                if (j == 1)
        //                {
        //                    stb.Append("<a class=\"a_num_CCl\" href=\"List.aspx?start=1\">" + j + "</a>");
        //                }
        //                else
        //                    stb.Append("<a class=\"a_num_CCl\" href=\"List.aspx?start=" + ((j - 1) * PageSize + 1) + "\">" + j + "</a>");
        //            }
        //        }
        //        stb.Append("<a class=\"a_num_CCl a_num_NCl\" href=\"#\">...</a>");
               
        //    }
        //        if ((startIndex + PageSize-1) >= TotalCount)
        //        {
        //            stb.Append("<a class=\"a_btn_CCl a_btn_NCl\" href=\"#\">下一页</a>");
        //        }
        //        else
        //            stb.Append("<a class=\"a_btn_CCl\" href=\"List.aspx?start=" + (startIndex + PageSize) + "\">下一页</a>");
        //    return stb.ToString();
        //}
        ///// <summary>
        ///// tb_size文本值改变事件
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //protected void tb_size_Click(object sender, EventArgs e)
        //{
        //    int _pageSize;
        //    if (int.TryParse(tb_size.Text, out _pageSize))
        //    { }
        //    else
        //        _pageSize = 8;
        //    DbCommon.Utils.writeToCookie("pageSize", (_pageSize).ToString());
        //    RpList_Bind();
        //}
        ///// <summary>
        ///// ddlUserClass文本改变事件
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //protected void ddlUserClass_Change(object sender, EventArgs e) {
        //    Session["UserClass"] = ddlUserClass.SelectedValue;
        //    RpList_Bind();
        //}
        ///// <summary>
        ///// dropdownlist控件绑定事件
        ///// </summary>
        ///// <param name="firstText"></param>
        ///// <param name="ddl"></param>
        //protected void ddlUserClass_Bind(string firstText,DropDownList ddl) {
        //    DataTable dt = userClass.GetUserClass();
        //    if (!string.IsNullOrEmpty(firstText)) {
        //        ddl.Items.Add(new ListItem(firstText, ""));
        //    }
        //    foreach (DataRow dr in dt.Rows) {
        //        ddl.Items.Add(new ListItem(dr["UserTitle"].ToString(), dr["UserId"].ToString()));
        //    }
        //}
        /// <summary>
        /// 批量删除事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int j = 0;
            for (int i = 0; i < RpList.Items.Count; i++) 
            {
                string id = ((HiddenField)RpList.Items[i].FindControl("Hf_id")).Value;
                CheckBox cb = (CheckBox)RpList.Items[i].FindControl("CbChoose");
                if (cb.Checked) 
                {
                    BoTeachers teachermodel = teacherService.GetTeacher(id);
                    teacherService.DeleteTeacher(teachermodel);
                    j++;
                }
            }
            if(j>0)
                Response.Write("<script>alert('删除成功，共删除" + j + "行数据');window.location.href='ListUser.aspx'</script>");
            else
                Response.Write("<script>alert('删除失败');window.location.href='ListUser.aspx'</script>");
        }
    }
}